package com.superhelper.task.job;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.superhelper.common.domain.AuserShop;
import com.superhelper.common.mapper.DeliverMapper;

@Component
public class ShopCache {
    private final static Logger LOG = LoggerFactory.getLogger(ShopCache.class);

    @Autowired
    protected DeliverMapper deliverConfMapper;
    private ListeningExecutorService executorService = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(1));
    private LoadingCache<String, Map<String, AuserShop>> shopIdTypeCache = CacheBuilder.newBuilder()
            .expireAfterAccess(10, TimeUnit.MINUTES).refreshAfterWrite(1, TimeUnit.HOURS)
            .build(new CacheLoader<String, Map<String, AuserShop>>() {
                @Override
                public Map<String, AuserShop> load(String key) throws Exception {
                    Map<String, AuserShop> shopIdTypeMap = new HashMap<>();
                    List<AuserShop> shops = deliverConfMapper.getAllUserShop();
                    for (AuserShop shop : shops) {
                        if (shop.getState() == 0) {
                            continue;
                        }
                        shopIdTypeMap.put(shop.getShopId() + "_" + shop.getShop_type(), shop);
                    }
                    return shopIdTypeMap;
                }

                @Override
                public ListenableFuture<Map<String, AuserShop>> reload(final String key,
                        Map<String, AuserShop> oldValue) throws Exception {
                    ListenableFuture<Map<String, AuserShop>> listenableFuture = executorService
                            .submit(new Callable<Map<String, AuserShop>>() {
                                @Override
                                public Map<String, AuserShop> call() throws Exception {
                                    return load(key);
                                }
                            });
                    LOG.info("load cache...........2");
                    return listenableFuture;
                }
            });

    public AuserShop getShop(String shopId, String shopType) {
        Map<String, AuserShop> map = null;
        try {
            map = shopIdTypeCache.get(StringUtils.EMPTY);
            if (map != null) {
                return map.get(shopId + "_" + shopType);
            }
        } catch (ExecutionException e) {
            LOG.error("getShop error", e);
        }
        return null;
    }

    public Collection<AuserShop> getAllShop() {
        Map<String, AuserShop> map = null;
        try {
            map = shopIdTypeCache.get(StringUtils.EMPTY);
            if (map != null) {
                return map.values();
            }
        } catch (ExecutionException e) {
            LOG.error("getShop error", e);
        }
        return Collections.emptyList();
    }
}
